﻿
using PublicCom;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace UpdateApp
{
    public partial class Template : System.Web.UI.Page
    {
        private TempHelper temp = new TempHelper();

        protected string File;
        protected string Content;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request["file"] != null)
            {
                File = Request["file"];

                if (File.IndexOf("/") == -1)
                {
                    string path = "/Template/" + File + ".html";

                    string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template/" + File + ".html");

                    File += ".aspx";

                    if (!System.IO.File.Exists(filePath))
                    {
                        FileInfo fileInfo = new FileInfo(filePath);
                        fileInfo.Create().Dispose();
                    }

                    ReadFile obj = new ReadFile(path);

                    if (IsPostBack)
                    {
                        obj.SaveAsHtml(path, Request.Form["editorValue"], FileMode.Create, Encoding.Default);

                        Response.Redirect(File);
                    }
                    else
                    {
                        this.html.Checked = true;
                        this.title.Value = Path.GetFileNameWithoutExtension(File);
                        Content = temp.ReadFile(obj, false);
                    }
                }
                else
                {
                    string path = File;

                    string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, File);

                    var arr = File.Split('/');
                    if (arr.Length == 3)
                        File = "Files.aspx?date=" + arr[1];
                    else
                        File = "Default.aspx";

                    if (!System.IO.File.Exists(filePath))
                    {
                        FileInfo fileInfo = new FileInfo(filePath);
                        fileInfo.Create().Dispose();
                    }

                    ReadFile obj = new ReadFile(path);

                    if (IsPostBack)
                    {
                        string ext = "." + Request.Form["type"];
                        string now = this.title.Value + ext;

                        //若改名就删除原文件
                        if (filePath.IndexOf(now, StringComparison.CurrentCultureIgnoreCase) == -1)
                            System.IO.File.Delete(filePath);

                        this.Content = ext == ".html" ? Request.Form["editorValue"] : this.myTextarea.Value;

                        Common.SaveFile(this.title.Value, this.Content, ext, arr[1]);

                        Response.Redirect(File);
                    }
                    else
                    {
                        this.title.Value = Path.GetFileNameWithoutExtension(path);

                        string dir = Path.Combine(FileHelper.GetDirName(), arr[1]);
                        var value = temp.ReadFile(obj, false, dir);

                        switch (Path.GetExtension(path))
                        {
                            case ".txt":
                                this.txt.Checked = true;
                                this.myTextarea.Value = value;
                                break;
                            case ".ini":
                                this.ini.Checked = true;
                                this.myTextarea.Value = value;
                                break;
                            default:
                                this.html.Checked = true;
                                this.Content = value;
                                break;
                        }

                        this.chk.Checked = path.StartsWith(FileHelper.GetTempDirName(), true, null);
                    }
                }
            }
            else
            {
                File = "Default.aspx";

                if (IsPostBack)
                {
                    string ext = "." + Request.Form["type"];
                    this.Content = ext == ".html" ? Request.Form["editorValue"] : this.myTextarea.Value;

                    if (!string.IsNullOrWhiteSpace(this.Content))
                    {
                        string folder = this.chk.Checked ? "temp" : DateTime.Now.ToString("yyyy-MM-dd");

                        Common.SaveFile(this.title.Value, this.Content, ext, folder);

                        //列表页点返回跳转到模板页
                        //Response.Redirect("Files.aspx?file=Template&date=" + folder);
                        Response.Redirect("Files.aspx?date=" + folder);
                    }
                }
                else
                {
                    this.html.Checked = true;
                    this.title.Value = DateTime.Now.ToString("MMddhhmm");
                }
            }
        }
    }
}